Imports System.Drawing.Drawing2D

Public Class FrmMain

	Dim WithEvents TreeMenu As Homepage
	Dim WithEvents Login As New Login
	Public WithEvents Tabs As New TabStrip With {.Height = 25, .Dock = DockStyle.Top}

	Public Sub New()
		'If Command() <> "" Then ForceBranch = Command()
		InitializeComponent()
		Themes.Register(Me)
		'Controls.Add(Tabs)
		Dim B As New Bitmap(32, 32)
		Dim G As Graphics = Graphics.FromImage(B)
		G.InterpolationMode = InterpolationMode.HighQualityBilinear
		G.PixelOffsetMode = PixelOffsetMode.HighQuality
		G.DrawImage(My.Resources.ssd2, New Rectangle(0, 0, B.Width, B.Height), New Rectangle(0, 0, My.Resources.ssd2.Width, My.Resources.ssd2.Height), GraphicsUnit.Pixel)
		Icon = Icon.FromHandle(B.GetHicon)
		Fix3DBorder()
		FixWindowSize()
		FixShadowDepth()
		'Tabs.Height = 26
		DoubleBuffered = True
		Controls.Add(Login)
		Login.Dock = DockStyle.Fill
	End Sub

	Protected Overrides Sub OnBackgroundImageChanged(e As System.EventArgs)
		MyBase.OnBackgroundImageChanged(e)
	End Sub

	Function AddWindow() As Window
		Dim Window As New Window
		SuspendLayout()
		Window.Tab = Tabs.AddTab("", Window)
		Controls.Add(Window)
		Tabs.SendToBack()
		Window.Dock = DockStyle.Fill
		ResumeLayout()
		Window.Tab.Activate()
		Return Window
	End Function

	Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
		Select Case keyData
			Case Keys.Control + Keys.T
				Tabs_TabAdded()
				Return True
		End Select
		Return MyBase.ProcessCmdKey(msg, keyData)
	End Function

	Private Sub Tabs_TabAdded() Handles Tabs.TabAdded
		Dim Window = AddWindow()
		Window.Launch(New Homepage)
	End Sub

	Private Sub Tabs_TabSelected() Handles Tabs.TabSelected
		Dim Window = Tabs.SelectedTab.Window
		If Window Is Nothing Then Exit Sub
		If Not Window.ActivePage Is Nothing Then Window.ActivePage.Focus()
		If Not Window.HasProcesses Then
			'Themes.Current.SetBackground(Me, 0)
			'If Window.Controls.Count > 0 Then Window.Controls(0).Focus()
		Else
			'Themes.Current.SetBackground(Me, 1)
			'Window.ActivePage.FocusLast()
		End If
	End Sub

	Sub Fix3DBorder()
		If My.Computer.Info.OSVersion < "5" OrElse My.Computer.Registry.CurrentUser.GetValue("Software\Microsoft\Windows\CurrentVersion\ThemeManager", "ThemeActive", "0") = "0" Then
			Padding = New Padding(2)
		End If
	End Sub

	Sub FixWindowSize()
		With My.Computer.Screen.WorkingArea
			If .Height <= 600 Then
				WindowState = FormWindowState.Maximized
			Else
				Size = New System.Drawing.Size((.Width + 800 * 3) / 4, (.Height + 600 * 3) / 4)
			End If
		End With
	End Sub

	Sub FixShadowDepth()
		ShadowDepth = If(My.Computer.Screen.BitsPerPixel > 16, 2, 1)
	End Sub

	Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
		MyBase.OnPaint(e)
		If Not Padding.Left = 2 Then Exit Sub
		ControlPaint.DrawBorder3D(e.Graphics, ClientRectangle)
	End Sub

	Sub ShowTab()
		Tabs.Visible = True
	End Sub

	Private Sub Login_LoginSuccessful(sender As Object) Handles Login.LoginSuccessful
		SuspendLayout()
		Controls.Remove(Login)
		Controls.Add(Tabs)
		Dim Window = AddWindow()
		Window.Launch(New Homepage)
		Tabs.ShowNewTab = True
		ResumeLayout()
	End Sub

End Class

